// Copyright (c) 2011 Blue Onion Software, All rights reserved
using System;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Web.UI.Design;
using System.Web.UI.WebControls;
using BlueOnionSoftware.Bloget.Properties;

namespace BlueOnionSoftware.Bloget
{
    [Designer(typeof (BlogetSearchDesigner))]
    public class BlogetSearch : Bloget
    {
        private readonly TextBox _textBox = new TextBox();
        private readonly Button _submit = new Button();
        private const int MaxFindLength = 30;
        private const string TextBoxId = "search_textbox";

        protected override void CreateChildControls()
        {
            const string space = " ";
            _textBox.ID = TextBoxId;
            _textBox.MaxLength = MaxFindLength;
            _textBox.Text = QuerySort;
            _textBox.TextChanged += GetSearchClick;
            Controls.Add(_textBox);

            Write(space);

            _submit.Click += GetSearchClick;
            _submit.Text = Text.ButtonPadding(Resources.SearchFindButton);
            Controls.Add(_submit);

            base.CreateChildControls();
        }

        private void GetSearchClick(object sender, EventArgs ea)
        {
            var query = BuildQueryString(Mode.Search, null, 0, null, _textBox.Text, default(DateTime), null);
            Redirect(query);
        }
    }

    internal class BlogetSearchDesigner : BlogetDesigner
    {
        public override string GetDesignTimeHtml()
        {
            return @"<input type=""text"" /> <input type=""button"" name="" Find "" />";
        }

        [SuppressMessage("Microsoft.Security", "CA2116:AptcaMethodsShouldOnlyCallAptcaMethods")]
        public override TemplateGroupCollection TemplateGroups
        {
            get
            {
                var collection = new TemplateGroupCollection();
                var control = (Bloget) Component;
                var group = new TemplateGroup("Bloget");
                var template = new TemplateDefinition(this, "IndexTemplate", control, "IndexTemplate", true);
                group.AddTemplateDefinition(template);
                collection.Add(group);
                return collection;
            }
        }
    }
}